package com.lancoo.answer.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by pengmi on 2022/2/15 9:47
 * Description:
 */
public class TranslateEditText extends AppCompatEditText {

    private int selectIndex=-1;
    private List<Integer> selectIndexList=new ArrayList<>();

    public TranslateEditText(@NonNull Context context) {
        super(context);
    }

    public TranslateEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TranslateEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);
        if(selectIndex==-1||selectIndex==0){
            selectIndex=selStart;
        }
        if(selectIndexList==null){
            selectIndexList=new ArrayList<>();
        }
        selectIndexList.add(selStart);
        if(selectIndexList.size()>4){
            selectIndexList.remove(0);
        }
        Log.e("eee","TranslateEditText onSelectionChanged:"+selStart);
    }

    public void resetSelectIndex(){
        selectIndex=-1;
        if(selectIndexList!=null){
            selectIndexList.clear();
        }
    }

    public int getSelectIndex(){
        if(selectIndexList!=null&&selectIndexList.size()>=2){
            Log.e("eee","获取json:"+new Gson().toJson(selectIndexList));
            return selectIndexList.get(selectIndexList.size()-2);
        }
        return selectIndex;
    }
}
